package com.damie.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClient;
import com.google.common.base.Predicates;
import lombok.Data;
import lombok.experimental.Accessors;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

@Configuration
// 指定配置文件中自定义属性前缀
@EnableSwagger2
@ConfigurationProperties(prefix = "aliyun")
@Data
@Accessors(chain = true)// 开启链式调用
public class AliyunOssConfig {
    private String endPoint="oss-cn-beijing.aliyuncs.com";// 地域节点
    private String accessKeyId="LTAI4G6ZkXmq6VWNHCXC5tc4";
    private String accessKeySecret="OeAOo7pEVSFK4xT98cNTuJvLLheDHz";
    private String bucketName="huwenlong223";// OSS的Bucket名称
    private String urlPrefix="http://huwenlong223.oss-cn-beijing.aliyuncs.com";// Bucket 域名
    private String fileHost="files";// 目标文件夹

    // 将OSS 客户端交给Spring容器托管
    @Bean
    public OSS OSSClient() {
        return new OSSClient(endPoint, accessKeyId, accessKeySecret);
    }

    @Bean
    public Docket webApiConfig() {
        ParameterBuilder ticketPar = new ParameterBuilder();
        List<Parameter> pars = new ArrayList<Parameter>();
        ticketPar.name("token").description("user ticket")//Token 以及Authorization 为自定义的参数，session保存的名字是哪个就可以写成那个
                .modelRef(new ModelRef("string")).parameterType("header")
                .required(false).build(); //header中的ticket参数非必填，传空也可以
        pars.add(ticketPar.build());    //根据每个方法名也知道当前方法在设置什么参数
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(webApiInfo())
                .enable(true)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.damie.controller"))//扫包
                .paths(Predicates.not(PathSelectors.regex("/error.*")))
                .build();
    }

    private ApiInfo webApiInfo() {
        return new ApiInfoBuilder()
                .title("用户管理")
                .description("服务中心")//描述
                .version("1.0")
                .contact(new Contact("Alon", "localhost:9095", "hwl_yt@163.com"))
                .build();
    }
}
